package com.njworkorder.Controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njworkorder.DTO.TestedProjectAddDto;
import com.njworkorder.Entity.ConstructionIndexItems;
import com.njworkorder.Entity.TestedProject;
import com.njworkorder.Service.TestedProjectService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import com.njworkorder.VO.TestedProjectListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

@Tag(name = "实测项目", description = "实测项目相关接口")
@RestController
@RequestMapping("/testedProject")
public class TestedProjectController {

    private final TestedProjectService testedProjectService;
    public TestedProjectController(TestedProjectService testedProjectService) {
        this.testedProjectService = testedProjectService;
    }

    @Operation(summary = "添加实测项目信息")
    @PostMapping("/addInfo")
    public Result<String> addInfo(@RequestBody TestedProjectAddDto testedProjectAddDto){

        TestedProject testedProject = new TestedProject();
        BeanUtils.copyProperties(testedProjectAddDto,testedProject);

        boolean save = testedProjectService.save(testedProject);
        if (save){
            return ResultUtil.success("添加成功");
        }
        return ResultUtil.error("添加失败");
    }

    @Operation(summary = "修改实测项目信息")
    @PostMapping("/updateById/{id}")
    public Result<String> updateInfo(@RequestBody TestedProjectAddDto testedProjectAddDto, @PathVariable String id){

        TestedProject testedProject = new TestedProject();
        BeanUtils.copyProperties(testedProjectAddDto,testedProject);
        testedProject.setId(id);
        boolean update = testedProjectService.updateById(testedProject);
        if (update){
            return ResultUtil.success("修改成功");
        }
        return ResultUtil.fail("修改失败");
    }

    @Operation(summary = "删除实测项目信息")
    @GetMapping("/deleteById")
    public Result<String> deleteInfo(@RequestParam String id){
        boolean removeById = testedProjectService.removeById(id);
        if (removeById){
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.fail("删除失败");
    }


    @Operation(summary = "实测项目信息列表")
    @GetMapping("/getList")
    public Result<Page<TestedProjectListVo>> getInfo(@RequestParam int pageIndex, @RequestParam int pageSize,
                                                     @Schema(description = "施工指标项id") @RequestParam("ciiId") String ciiId){

        MPJLambdaWrapper<TestedProject> wrapper = new MPJLambdaWrapper<TestedProject>()
                .selectAll(TestedProject.class)
                .leftJoin(ConstructionIndexItems.class, ConstructionIndexItems::getId,TestedProject::getCiiId,ext->
                        ext.selectAs(ConstructionIndexItems::getIndexItemsName,TestedProjectListVo::getIndexItemsName))
                .apply("1=1")
                .eq(StringUtils.isNotEmpty(ciiId),TestedProject::getCiiId,ciiId);

        Page<TestedProjectListVo> testedProjectListVoPage = testedProjectService.selectJoinListPage(new Page<>(pageIndex, pageSize), TestedProjectListVo.class, wrapper);

        return ResultUtil.success(testedProjectListVoPage);
    }




}
